package sqlsource.impl;

import mapping.BoundSql;
import mapping.DynamicContext;
import sqlnode.SqlNode;
import sqlsource.SqlSource;
import sqlsource.SqlSourceParser;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2020-01-04 14:52
 * @notify #{} 中的内容进行处理 RawSqlSource只包含 StaticTextSqlNode
 * @version 1.0
 */
public class RawSqlSource implements SqlSource {

    private SqlNode mixedSqlNode;

    public RawSqlSource(SqlNode mixedSqlNode) throws Exception {
        this.mixedSqlNode = mixedSqlNode;
    }

    @Override
    public BoundSql getBoundSql(Object param) throws Exception {
        //执行该sqlSource中的所有sqlNode
        DynamicContext dynamicContext = new DynamicContext(null);
        mixedSqlNode.apply(dynamicContext);
        //将 #{ } 替换成 ？ 然后将内容存储到 ParameterMapping 中
        SqlSourceParser parser = new SqlSourceParser(dynamicContext);
        SqlSource sqlSource = parser.parse();
        return sqlSource.getBoundSql(param);
    }

}
